<?php
	//include("../write_functions/write_functions.php");
	//connect('localhost','root','','gradegriddatabase');
	
	function fetchAccountDetails(){
		$que = "SELECT *
				FROM account
				WHERE username = '".$_SESSION['username']."'";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $data = mysql_fetch_array($result);
	}
	
	function fetchSchoolDetails($schoolId){
		$que = "SELECT *
				FROM school
				WHERE school_id = '".$schoolId."'";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $data = mysql_fetch_array($result);
	}
	
	function fetchTemplateDetails($templateId){
		$que = "SELECT template.template_id as template_id, account_id, template_name, minor.major_id, major.name as major_name, percentage, minor_id, minor.name as minor_name, maxrawscore
			FROM template
			INNER JOIN major ON template.template_id = major.template_id
			INNER JOIN minor ON major.major_id = minor.major_id
			AND template.template_id = '".$templateId."'
			GROUP BY minor.major_id, minor.minor_id
			ORDER BY minor.major_id, minor.minor_id";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $result;
	}
	
	function fetchMostRecentTemplateId($templateName){
		$que = "SELECT template_id FROM template WHERE template_name = '".$templateName."' GROUP BY template_id ORDER BY template_id DESC LIMIT 1";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $data = mysql_fetch_array($result);
	}
	
	function fetchMostRecentMajorId($templateId){
		$que = "SELECT major_id FROM major WHERE template_id = '".$templateId."' GROUP BY major_id ORDER BY major_id DESC LIMIT 1";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $data = mysql_fetch_array($result);
	}
	
	function fetchAccountClassJoin(){
		$que = "SELECT class_id, course_name, class_name
				FROM class, course, account WHERE
				class.course_id = course.course_id AND class.account_id = account.account_id
				AND account.username = '".$_SESSION['username']."'";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $result;
	}
	
	function fetchAccountTemplateJoin(){
		$que = "SELECT *
				FROM account INNER JOIN template
				ON account.account_id = template.account_id
				AND account.username = '".$_SESSION['username']."'";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $result;
	}
	
	function fetchAccountSchoolJoin(){
		$que = "SELECT *
				FROM account INNER JOIN school
				ON account.account_id = school.account_id
				AND account.username = '".$_SESSION['username']."'";
		$result = mysql_query($que);
		if (!$result){
			die('Error: ' . mysql_error());
		}
		return $result;
	}
?>